<?php
    
    include_once('classes/class.mssql.php');
    
    session_start();
    
    $db = new MSSQL();
    
    $comtec_id_user = $_GET['id']; 
    
    date_default_timezone_set('America/Santiago');
    $op_day = date("d");
    $op_month = date("m");
    $op_year = date("Y");
    $date = $op_year.$op_month.$op_day." 00:00:00";
    
    if($_SESSION["users"]!="clients"){
     $stmt = $db->query("SELECT DATEADD(day,5,'$date') as date");
    }else
        {
         $stmt = $db->query("SELECT DATEADD(day,30,'$date') as date"); 
        } 
        
    sqlsrv_fetch($stmt);    
    $date2 = sqlsrv_get_field($stmt,0,SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
    $next_day = substr($date2,8,2);
    $next_month = substr($date2,5,2);
    $next_year = substr($date2,0,4);
    
    $date  = $op_day."/".$op_month."/".$op_year; 
    $date2 = $next_day."/".$next_month."/".$next_year;
    if($_SESSION["users"]!="clients"){
    $tsql =  " SELECT  (r.name+' '+r.lastname1+' '+r.lastname2) as encargado,m.comtec_id_machine,m.cod_mainteiner,m.brand,m.model,m.year,p.company_name,CONVERT(varchar,m.date_next_maintenance,103) as date";
    $tsql .= " FROM ((machines as m INNER JOIN [users] as u ON m.comtec_id_mainteiner = u.comtec_id_mainteiner) 
                INNER JOIN owner as p ON m.comtec_id_owner = p.comtec_id_owner) LEFT JOIN [responsible] as r ON 
                m.comtec_id_responsible = r.comtec_id_responsible";
    $tsql .= " WHERE u.comtec_id_user=$comtec_id_user AND m.status_reg=1 AND 
               m.date_next_maintenance >= '$date' AND m.date_next_maintenance <= '$date2' 
               ORDER BY m.date_next_maintenance ASC  ";
    }else{
          $tsql  = "SELECT  (r.name+' '+r.lastname1+' '+r.lastname2) as encargado,m.comtec_id_machine,m.cod_mainteiner,m.brand,m.model,m.year,p.company_name,CONVERT(varchar,m.date_next_maintenance,103) as date";
          $tsql .= " FROM ((machines as m INNER JOIN [users] as u ON m.comtec_id_mainteiner = u.comtec_id_mainteiner) 
                     INNER JOIN owner as p ON m.comtec_id_owner = p.comtec_id_owner) LEFT JOIN [responsible] as r ON 
                     m.comtec_id_responsible = r.comtec_id_responsible";
          $tsql .= " WHERE p.comtec_id_user=$comtec_id_user AND u.comtec_id_user=$comtec_id_user AND m.status_reg=1 AND 
                     m.date_next_maintenance >= '$date' AND m.date_next_maintenance <= '$date2' 
                     ORDER BY m.date_next_maintenance ASC";
    }
    
    $stmt = $db->query($tsql);  
    $texto = "<table class='table'>
              <tr>
              <th></th>
              <th>C&oacute;digo</th>
              <th>Marca</th>
              <th>Modelo</th>
              <th>A&ntilde;o</th>
              <th>Propietario</th>   
              <th>Encargado</th>
              <th>Fecha ult. oper.</th>
              <th>Fecha prox. oper.</th>
              </tr>";  
    $cont = 0;
    while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){ 
     $cont += 1; 
     $comtec_id_machine=$row['comtec_id_machine'];
     $tsql = "SELECT TOP 1  CONVERT(varchar,operation_date_end,103) as last_operation
               FROM operation
               WHERE comtec_id_machine=$comtec_id_machine
               ORDER BY operation_date_end DESC";  
     $row1=sqlsrv_fetch_array($db->query($tsql),SQLSRV_FETCH_ASSOC);
     $last_operation = $row1['last_operation']; 
     
     $cod_mainteiner=$row['cod_mainteiner'];
     $brand=$row['brand'];
     $model=$row['model'];
     $year=$row['year'];
     $date=$row['date'];
     $company_name=$row['company_name'];
     $encargado=$row['encargado'];
     $texto  .= "<tr>";
     $url_view = 'http://'.$_SERVER['HTTP_HOST'].'/iconos/viewMaintenance.png';
     $texto  .= "<td><a href='javascript:loadOperationsMachine(".$comtec_id_machine.");'><img src='$url_view' alt='Ver operaciones' style='border: 0px;' /></a></td>";
     $texto .= "<td><a href='javascript:machine(".$comtec_id_machine.");'>$cod_mainteiner</a></td>";
     $texto  .= "<td>$brand</td>";
     $texto  .= "<td>$model</td>";
     $texto  .= "<td>$year</td>";
     $texto  .= "<td>$company_name</td>";
     $texto  .= "<td>$encargado</td>";
     $texto  .= "<td>$last_operation</td>";
     $texto  .= "<td>$date</td>"; 
     $texto .= "</tr>";
    }
     $texto .= "</table>";
     echo "<p class='total'>Total: $cont</p>".$texto;     
?>
